﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.Common;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.CustomerForms
{
    public partial class CustomerAddForm : DevExpress.XtraEditors.XtraForm
    {
        Action InitGrid;
        BLL.CustomerBll customerBll = new BLL.CustomerBll();
        private int _option;
        private Customer _customer;
        public CustomerAddForm(Action InitGrid )
        {
            InitializeComponent();
            this.InitGrid = InitGrid;
            _option = 1;
            this.Text = ResourceOverall.CreateStr;
        }

        public CustomerAddForm(Customer customer, Action InitGrid)
        {
            InitializeComponent();
            _option = 2;
            this.Text = ResourceOverall.EditStr;
            _customer = customer;
            InitEditModel(_customer);
            this.InitGrid = InitGrid;
        }

        private void InitEditModel(Customer customer)
        {
            this.textEditCustomerName.Text = customer.CustomerName;
            this.textEditTel.Text = customer.Tel;
            this.textEditAddress.Text = customer.Address;
            this.textEditLinkMan.Text = customer.LinkMan;
            this.textEditLinkManMobile.Text = customer.LinkManMobile;
            this.textEditRemark.Text = customer.Remark;
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
           

            if (_option==1)
            {
                if (this.textEditCustomerName.Text == "")
                {
                    textEditCustomerName.Focus();
                    XtraMessageBox.Show(ResourceOverall.InputNull);
                    return;
                }
                //判断是否重复
                var cust = customerBll.LoadEntities(m => m.CustomerName == textEditCustomerName.Text);
                if (cust.Count() > 0)
                {
                    XtraMessageBox.Show(ResourceOverall.DataExists);
                    return;
                }
                Customer customer = new Customer();
                customer.CustomerID = Guid.NewGuid().ToString();
                customer.Address = this.textEditAddress.Text;
                customer.CustomerName = this.textEditCustomerName.Text;
                customer.LinkMan = this.textEditLinkMan.Text;
                customer.LinkManMobile = this.textEditLinkManMobile.Text;
                customer.Remark = this.textEditRemark.Text;
                customer.Tel = this.textEditTel.Text;
                customer.AddDateTime = DateTime.Now;
                bool res = AddCustomerAndStandard(customer);
                if (res)
                {
                    this.Close();
                }
            }
            if (_option == 2)
            {
               
                using (OJCB4Entities db =new OJCB4Entities())
                {
                    Customer customer = db.Customers.Find(_customer.CustomerID);
                    customer.Address = this.textEditAddress.Text;
                    customer.CustomerName = this.textEditCustomerName.Text;
                    customer.LinkMan = this.textEditLinkMan.Text;
                    customer.LinkManMobile = this.textEditLinkManMobile.Text;
                    customer.Remark = this.textEditRemark.Text;
                    customer.Tel = this.textEditTel.Text;
                    bool res = db.SaveChanges()>0;
                    if (res)
                    {
                        InitGrid();
                        XtraMessageBox.Show(ResourceOverall.EditSucceed);
                        this.Close();
                    }
                }
            }

           

        }

        private void CustomerAddForm_Load(object sender, EventArgs e)
        {

        }

        private bool AddCustomerAndStandard(Customer customer)
        {
            int count = customerBll.LoadEntities(m => m.CustomerName == customer.CustomerName).Count();
            if (count > 0)
            {
                XtraMessageBox.Show(ResourceOverall.DataExists);
                return false;
            }
            splashScreenManager1.ShowWaitForm();
            bool res = customerBll.AddCustomerAndStandard(customer);
            splashScreenManager1.CloseWaitForm();
            if (res)
            {
                InitGrid();
                XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                return true;
            }
            else
            {
                XtraMessageBox.Show(ResourceOverall.CreateFailed);
                return false;
            }

        }

    }
}